package com.chengduservice.service.impl;

import com.chengduservice.common.Page;
import com.chengduservice.common.QueryPage;
import com.chengduservice.dao.NewsDAO;
import com.chengduservice.entity.News;
import com.chengduservice.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * create by yongli on 2020-04-16 21:55
 */

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    NewsDAO newsDAO;


    @Override
    public Integer batchInsertNews(List<News> newsList) {
        return newsDAO.batchInsertNews(newsList);
    }

    @Override
    public Integer insertOneNews(News news) {
        return newsDAO.insertOneNews(news);
    }

    @Override
    public List<News> selectByKeyWord(String keyWord) {
        return newsDAO.selectByKeyWord(keyWord);
    }

    @Override
    public List<News> batchSelect(Map<String, String> map) {
        return newsDAO.batchSelect(map);
    }

    @Override
    public Page<News> listPage(QueryPage<News> queryPage) {
        //        在mybatis中没有Page对象，那么mybatis中怎么分页呢？
//        IPage<News> page = new Page(queryPage.getCurrent(),queryPage.getSize());getSize
        long cur = queryPage.getCurrent();
        long size = queryPage.getSize();
        Page<News> page = new Page<>(queryPage.getCurrent(), queryPage.getSize());
        long start = (cur - 1) * size;
        List<News> data = newsDAO.listPage(queryPage.getQuery(), start, size);
        page.setData(data);
        page.setTotal(newsDAO.count());
        return page;

    }

    @Override
    public long count() {
        return newsDAO.count();
    }
}
